<template>
    <el-dialog :close-on-click-modal="false"  ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="35%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item label="计划名称" prop="workPlanName">
                <el-input :disabled="ruleForm.status == 'INVALID'" v-model="ruleForm.workPlanName" class="w100p"
                    placeholder="请输入计划名称"></el-input>
            </el-form-item>
            <el-form-item label="计划有效时间" prop="datetime">
                <el-date-picker :disabled="ruleForm.status == 'INVALID'" v-model="ruleForm.datetime"
                    type="datetimerange" value-format="yyyy-MM-dd HH:mm:ss" :picker-options="pickerOptions"
                    range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" align="right">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="计划状态" v-if="ruleForm.status !== 'INVALID'">
                <el-switch v-model="ruleForm.status" active-color="#13ce66" inactive-color="#ff4949"
                    :active-value="'ENABLE'" :inactive-value="'DISABLE'" active-text="启用" inactive-text="停用">
                </el-switch>
            </el-form-item>
            <el-form-item label="计划状态" v-else>
                <span>计划失效</span>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary"  @click="submitForm" v-if="ruleForm.status !== 'INVALID'">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import operationPlanManagement from '@/api/jobManagment/operationPlanManagement';
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        }
    },
    data() {
        return {
            pickerOptions: {
                disabledDate(time) {
                    return time.getTime() < Date.now() - 8.64e7;
                },
                shortcuts: [{
                    text: '一周',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() + 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', [end, start]);
                    }
                }, {
                    text: '一个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() + 3600 * 1000 * 24 * 30);
                        picker.$emit('pick', [end, start]);
                    }
                }, {
                    text: '三个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() + 3600 * 1000 * 24 * 90);
                        picker.$emit('pick', [end, start]);
                    }
                }]
            },
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {},
            rules: {
                workPlanName: [
                    { required: true, message: '请输入计划名称', trigger: 'change' }
                ],
                status: [
                    { required: true, message: '请选择', trigger: 'change' }
                ],
                datetime: [
                    { required: true, message: '请选择时间', trigger: 'change' }
                ]
            }
        }
    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },

    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm();
                this.ruleForm = {
                    status: 'ENABLE'
                };
                this.addEditApi = operationPlanManagement.add;
                this.addEditName = '添加';
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = {
                    ...this.data,
                    datetime: [this.data.startTime, this.data.endTime]
                };
                this.addEditApi = operationPlanManagement.edit;
                this.addEditName = '编辑';

            }
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    let params = {
                        startTime: this.ruleForm.datetime[0],
                        endTime: this.ruleForm.datetime[1]
                    }
                    delete this.ruleForm.datetime
                    this.addEditApi({ ...this.ruleForm, ...params }).then(res => {
                        let msg = res.code == 1 ? '操作成功' : '操作失败'
                        this.$message[res.code == 1 ? 'success' : 'warning'](res.msg || msg)
                        res.code == 1 && this.cancel();
                    }).catch(err => {
                        console.log(err)
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped>
.line {
    border-bottom: 1px solid #e6e6e6;
}

.icon {
    font-size: 20px;
    line-height: 29px;
    cursor: pointer;
}

::v-deep .noLabel {
    .el-form-item {
        .label {
            display: none;
        }

        .el-form-item__content {
            margin: 0;
        }
    }
}


.elRow {
    text-align: center;
}
</style>